import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import java.io.*;
import java.util.*;

public class demonstrator extends JFrame implements ActionListener{

	private JPanel panel;
	private JButton pause, go, speedup, speeddown;
	private JLabel speedNow;
	private Robot myRobs[];
	private boolean keepGoing = false;
	private int frameSpeed = 5;
	int numberOfRobs = 9;
	
	public static void main(String[] args) {

		int size = 500;
		
		demonstrator frame = new demonstrator();
		frame.setSize(size + 500, size + 100);
		frame.createGUI(size);
		frame.show();
		
		frame.run();

	}

	private void createGUI(int size){
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		Container window = getContentPane();
		window.setLayout(new FlowLayout());
		
	    panel = new JPanel();
	    panel.setPreferredSize(new Dimension(size,size));
	    panel.setBackground(Color.WHITE);
	    window.add(panel);
	   
	    
	    go = new JButton("GO");
	    window.add(go);
	    go.addActionListener(this);
	    
	    pause = new JButton("Pause");
	    window.add(pause);
	    pause.addActionListener(this);
	    
	    speedup = new JButton("Speed UP");
	    window.add(speedup);
	    speedup.addActionListener(this);
	    
	    speeddown = new JButton("Speed Down");
	    window.add(speeddown);
	    speeddown.addActionListener(this);
	    
	    speedNow = new JLabel(" " + frameSpeed + " ");
	    window.add(speedNow);
	}
	
	private void run(){
		
		myRobs = new Robot[numberOfRobs];
		for(int i = 0; i < numberOfRobs; i++)
			myRobs[i] = new Robot(panel, 0, 0, 0, true);
		
		BufferedReader input[]= new BufferedReader[numberOfRobs];
		String line[] = new String[numberOfRobs];
		boolean hasLine = true;
		
		try{
			for(int i = 0; i < numberOfRobs; i ++)
				input[i] = new BufferedReader(new FileReader("robotLog" + i + ".txt"));
			while(hasLine){
//				panel.getGraphics().setColor(Color.BLACK);
				panel.getGraphics().fillRect(300, 300, 10, 10);
				
				if(!keepGoing){
					for(int i = 0; i < numberOfRobs; i ++)
						myRobs[i].draw();
					try{
						Thread.sleep(5 * frameSpeed);
					}
					catch(InterruptedException e){
						return;
					}
					
					for(int i = 0; i < numberOfRobs; i ++)
						myRobs[i].delete();
					continue;
				}	
				for(int i = 0; i < numberOfRobs; i ++){
					if((line[i] = input[i].readLine()) == null)
						hasLine = false;
					StringTokenizer tokens = new StringTokenizer(line[i]);
					myRobs[i].setX(Double.parseDouble(tokens.nextToken()));
					myRobs[i].setY(Double.parseDouble(tokens.nextToken()));
					myRobs[i].setDirection(Double.parseDouble(tokens.nextToken()));
					myRobs[i].setStatus(Integer.parseInt(tokens.nextToken()));
				}
				
				for(int i = 0; i < numberOfRobs; i ++)
					myRobs[i].draw();
				
				try{
					Thread.sleep(5 * frameSpeed);
				}
				catch(InterruptedException e){
					return;
				}
				
				for(int i = 0; i < numberOfRobs; i ++)
					myRobs[i].delete();
				
			}
			for(int i = 0; i < numberOfRobs; i ++)
				input[i].close();
		}
		catch(FileNotFoundException ex){
			ex.printStackTrace();
		}
		catch(IOException ex){
			ex.printStackTrace();
		}

	}
	
	public void actionPerformed(ActionEvent event){
		if (event.getSource()== go)
			this.keepGoing = true;
	    
		if(event.getSource()== pause)
		   this.keepGoing = false;
	    
		if(event.getSource()==speeddown)
			if(frameSpeed < 10){
				frameSpeed ++;
				speedNow.setText(" " + (11 - frameSpeed) + " ");
			}
		if(event.getSource()==speedup)
			if(frameSpeed > 1){
				frameSpeed --;
				speedNow.setText(" " + (11 - frameSpeed) + " ");
			}
	    
	  }
	

}
